package com.dwxt.community.controller;

import com.dwxt.common.annotation.ValidateFiled;
import com.dwxt.common.annotation.ValidateGroup;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.util.DateUtils;
import com.dwxt.community.entity.IntegralRecord;
import com.dwxt.community.entity.IntegralRule;
import com.dwxt.community.service.IntegralService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @program: intelligentcity
 * @description: 垃圾投放积分处理控制层
 * @author
 * @create: 2020/04/14
 */
@RequestMapping("/cms/integral")
@RestController
public class IntegralController {

    @Autowired
    private IntegralService integralService;

    /**
     * showdoc
     *
     * @param pageNum  非必须 Integer 当前页
     * @param pageSize 非必须 Integer 每页显示条数
     * @param type     非必须 String 垃圾类型
     * @param str      非必须 String 开始时间
     * @param end      非必须 String 结束时间
     * @return {"status":200,"msg":"SUCCESS","data":{"total":2,"list":[{"tirId":1,"userId":"158502014539731","createTime":"2020-04-14T14:57:47.000+0000","delivererId":"158502014539731","gotIntegral":20,"totalIntegral":500,"trashType":1,"deliverWeight":20},{"tirId":2,"userId":"158502014539731","createTime":"2020-04-14T14:58:10.000+0000","delivererId":"158502014539731","gotIntegral":15,"totalIntegral":515,"trashType":1,"deliverWeight":15}],"pageNum":1,"pageSize":10,"size":2,"startRow":1,"endRow":2,"pages":1,"prePage":0,"nextPage":0,"isFirstPage":true,"isLastPage":true,"hasPreviousPage":false,"hasNextPage":false,"navigatePages":8,"navigatepageNums":[1],"navigateFirstPage":1,"navigateLastPage":1}}
     * @catalog 社区/环保
     * @description 根据 户主查询 积分记录
     * @url /cms/integral/getByHomeAddr
     */
    @GetMapping("/getByHomeAddr")
    public BaseResult get(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "10") Integer pageSize,
                          Integer type,
                          String str,
                          String end) {
        return integralService.getByHomeAddr(pageNum, pageSize, type, str, end);
    }

    /**
     * showdoc
     *
     * @param pageNum  非必须 Integer 当前页
     * @param pageSize 非必须 Integer 每页显示条数
     * @param type     非必须 String 垃圾类型
     * @param str      非必须 String 开始时间
     * @param end      非必须 String 结束时间
     * @return {"status":200,"msg":"SUCCESS","data":{"total":2,"list":[{"tirId":1,"userId":"158502014539731","createTime":"2020-04-14T14:57:47.000+0000","delivererId":"158502014539731","gotIntegral":20,"totalIntegral":500,"trashType":1,"deliverWeight":20},{"tirId":2,"userId":"158502014539731","createTime":"2020-04-14T14:58:10.000+0000","delivererId":"158502014539731","gotIntegral":15,"totalIntegral":515,"trashType":1,"deliverWeight":15}],"pageNum":1,"pageSize":10,"size":2,"startRow":1,"endRow":2,"pages":1,"prePage":0,"nextPage":0,"isFirstPage":true,"isLastPage":true,"hasPreviousPage":false,"hasNextPage":false,"navigatePages":8,"navigatepageNums":[1],"navigateFirstPage":1,"navigateLastPage":1}}
     * @catalog 社区/环保
     * @description 根据 投递者 查询 积分记录
     * @url /cms/integral/get
     */
    @GetMapping("/get")
    public BaseResult getByDeliverer(@RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "10") Integer pageSize,
                                     Integer type,
                                     String str,
                                     String end) throws ParseException {
        return integralService.getByDeliverer(pageNum, pageSize, type, DateUtils.formatDateStr(str), DateUtils.formatDateStr(end));
    }

    /**
     * showdoc
     *
     * @param type 非必须 Integer 垃圾类型
     * @return {"status":200,"msg":"SUCCESS","data":[{"alreadyNum":0,"todayGotIntegralNum":0,"trashName":"厨余垃圾","deliveMax":2,"totleIntegral":0,"gotIntegralNum":0,"maxIntegral":0}]}
     * @catalog 社区/环保
     * @description 获取当天的垃圾投放情况
     * @url /cms/integral/getTrashTodayData
     */
    @GetMapping("/getTrashTodayData")
    public BaseResult getTrashTodayData() {
        return integralService.getTrashTodayData();
    }

    /**
     * showdoc
     *
     * @return {"status":200,"msg":"SUCCESS","data":{"alreadyNum":0,"todayGotIntegralNum":100,"deliveMax":0,"totleIntegral":100,"gotIntegralNum":35,"maxIntegral":0}}
     * @catalog 社区/环保
     * @description 获取当天的积分获取情况
     * @url /cms/integral/integralDetail
     */
    @GetMapping("/integralDetail")
    public BaseResult getIntegralDetail() {
        return integralService.getIntegralDetail();
    }

    /**
     * showdoc
     *
     * @return {"status":200,"msg":"SUCCESS","data":[{"id":1,"strTime":"2020-04-14T16:53:34.000+0000","endTime":"2020-04-14T11:00:00.000+0000","createTime":"2020-04-14T16:53:26.000+0000","updateTime":"2020-04-14T16:53:26.000+0000"}]}
     * @catalog 社区/环保
     * @description 获取投放时间段
     * @url /cms/integral/integralTimely
     */
    @GetMapping("/integralTimely")
    public BaseResult getIntegralTimely() {
        return BaseResult.ok(integralService.getIntegralTimely());
    }

    /**
     * showdoc
     *
     * @return {"status":200,"msg":"SUCCESS","data":[{"id":1,"strTime":"2020-04-14T16:53:34.000+0000","endTime":"2020-04-14T11:00:00.000+0000","createTime":"2020-04-14T16:53:26.000+0000","updateTime":"2020-04-14T16:53:26.000+0000"}]}
     * @catalog 社区/环保
     * @description 获取垃圾类别列表
     * @url /cms/integral/trashCategory
     */
    @GetMapping("/trashCategory")
    public BaseResult getTrashCategory() {
        return BaseResult.ok(integralService.getTrashCategory());
    }

    /**
     * showdoc
     *
     * @param pageNum  必选 int  页码(1开始)
     * @param pageSize 必选 int 一页数量
     * @return {"status":200,"msg":"SUCCESS","data":{"total":2,"list":[{"id":12,"integral":100,"category":"注册","createTime":"2020-04-2014:42:24","userId":"158736494349365","relateId":null,"year":2020,"month":4,"day":20},{"id":2,"integral":-1,"category":"shop","createTime":"2020-04-1010:48:21","userId":"158736494349365","relateId":null,"year":2020,"month":4,"day":10}],"pageNum":1,"pageSize":10,"size":2,"startRow":1,"endRow":2,"pages":1,"prePage":0,"nextPage":0,"isFirstPage":true,"isLastPage":true,"hasPreviousPage":false,"hasNextPage":false,"navigatePages":8,"navigatepageNums":[1],"navigateFirstPage":1,"navigateLastPage":1}}
     * @catalog 社区/积分
     * @title 积分记录分页
     * @description 积分记录分页查询
     * @url /cms/integral/selectIntegralPage
     */
    @RequestMapping("/selectIntegralPage")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, notNull = true),
            @ValidateFiled(index = 1, notNull = true),
    })
    public BaseResult selectIntegralPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<IntegralRecord> recordList = integralService.selectIntegralRecordList();
        PageInfo<IntegralRecord> pageInfo = new PageInfo<>(recordList);
        return BaseResult.ok(pageInfo);
    }

    /**
     * showdoc
     *
     * @param year  必选 int 年
     * @param month 必选 int 月
     * @return {"status":200,"msg":"SUCCESS","data":{"income":10,"outlay":3}}
     * @catalog 社区/积分
     * @title 查询某个月份的积分消耗和收入
     * @description 查询某个月份的积分消耗和收入
     * @url /cms/integral/selectMonthIntegral
     */
    @RequestMapping("/selectMonthIntegral")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, notNull = true),
            @ValidateFiled(index = 1, notNull = true),
    })
    public BaseResult selectMonthIntegral(Integer year, Integer month) {
        Map<String, Integer> map = integralService.selectMonthIntegral(year, month);
        return BaseResult.ok(map);
    }

    /**
     * showdoc
     *
     * @return {"status":200,"msg":"SUCCESS","data":1}
     * @catalog 社区/积分
     * @title 查询积分排名
     * @description 查询积分排名
     * @url /cms/integral/selectIntegralRank
     */
    @RequestMapping("/selectIntegralRank")
    public BaseResult selectIntegralRank() {
        Integer rank = integralService.selectIntegralRank();
        return BaseResult.ok(rank);
    }

    @RequestMapping("/selectRuleByCategory")
    public BaseResult selectRuleByCategory(String category) {
        IntegralRule integralRule = integralService.selectRuleByCategory(category);
        return BaseResult.ok(integralRule);
    }
}
